#!/bin/bash

# 安装依赖
yum install wget curl sudo ntpdate -y

# 关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service

# 关闭 SELinux
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
setenforce 0

# 添加定时任务
echo "$((RANDOM%60)) $((RANDOM%24)) * * * /usr/sbin/ntpdate time1.aliyun.com" >> /var/spool/cron/root

# 禁用透明大页面压缩
echo never > /sys/kernel/mm/transparent_hugepage/defrag
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo "#!/bin/bash" > /etc/rc.d/rc.local
echo "echo never > /sys/kernel/mm/transparent_hugepage/defrag" >> /etc/rc.d/rc.local
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
systemctl start rc-local

# 优化交换分区
echo "vm.swappiness = 10" >> /etc/sysctl.conf
sysctl -p

# 安装 Apache httpd
yum -y install httpd
systemctl enable httpd.service
systemctl start httpd.service

# 安装 createrepo
yum -y install yum-utils createrepo

# 下载 Cloudera Manager 安装包和 Yum 源
mkdir -p /var/www/html/cm
cd /var/www/html/cm
wget http://45.78.34.91/cm/cloudera-manager-installer.bin
wget http://45.78.34.91/cm/allkeys.asc
mkdir -p /var/www/html/cm/yum
cd /var/www/html/cm/yum
wget http://45.78.34.91/cm/yum/cloudera-manager-agent-6.2.0-968826.el7.x86_64.rpm
wget http://45.78.34.91/cm/yum/cloudera-manager-daemons-6.2.0-968826.el7.x86_64.rpm
wget http://45.78.34.91/cm/yum/cloudera-manager-server-6.2.0-968826.el7.x86_64.rpm
wget http://45.78.34.91/cm/yum/cloudera-manager-server-db-2-6.2.0-968826.el7.x86_64.rpm
wget http://45.78.34.91/cm/yum/enterprise-debuginfo-6.2.0-968826.el7.x86_64.rpm

# 获取管理节点 IP
read -p "请输入管理节点 IP：" manager_ip

# 创建本地 Yum 源
createrepo /var/www/html/cm/
echo "[cloudera-manager]" > /etc/yum.repos.d/cloudera-manager.repo
echo "name=Cloudera Manager, Version yum" >> /etc/yum.repos.d/cloudera-manager.repo
echo "baseurl=http://$manager_ip/cm" >> /etc/yum.repos.d/cloudera-manager.repo
echo "gpgcheck=0" >> /etc/yum.repos.d/cloudera-manager.repo
echo "enabled=1" >> /etc/yum.repos.d/cloudera-manager.repo

# 更新 Yum 缓存
yum clean all
yum makecache
